//入口文件
import Vue from 'vue';

// 1.1 导入路由的包
import VueRouter from 'vue-router'
// 1.2 安装路由
Vue.use(VueRouter)
//引入自己的router
import router from './router.js'

//导入vuex
import Vuex from 'vuex';
Vue.use(Vuex);
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    //添加购物车
    addToCar: function (state, goodsinfo) {
      //假设没有找到商品
      var flag = false;
      //判断购物车有没有这个商品
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count += parseInt(goodsinfo.count);
          //有商品
          flag = true;
          return true
        }
      })
      if (!flag) {
        state.car.push(goodsinfo)
      }
      // 当 更新 car 之后，把 car 数组，存储到 本地的 localStorage 中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    //更新总数
    updataCount: function (state, goodsinfo) {
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    //移除商品
    removeCart(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    //更新商品选中状态
    updateSelStatue(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selSatus = info.sel
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.count
      })
      return c
    },
    getGoodsCount(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.count
      })
      return o
    },
    getSelStatus(state) {
      var o = {}
      state.car.forEach(item => {
        o[item.id] = item.selSatus
      })
      return o
    },
    getMount(state) {
      var obj = {
        price: 0,
        amount: 0
      }
      state.car.forEach(item => {
        if (item.selSatus) {
          obj.amount += item.count
          obj.price += item.count * item.price
        }
      })
      return obj
    }
  }
})

//引入请求资源文件
import VueResource from 'vue-resource';
Vue.use(VueResource)
Vue.http.options.root = 'http://localhost:3005';

//使用缩略图
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(preview)

//导入mui样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

//按需导入mint组件
import { Header, Swipe, SwipeItem, Button, Lazyload, Search, Switch } from 'mint-ui';
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.component(Search.name, Search);
Vue.component(Switch.name, Switch);
Vue.use(Lazyload);

//时间过滤器
import moment from 'moment';
Vue.filter('dataFormat', function (str, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(str).format(pattern)
})

//导入app根组件
import app from './App.vue'

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})
